﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class MonDAO
    {
        // Operation...
        public static bool ThemMon(MonDTO mon)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Insert into MON(TenMon, NhomMon, DonViTinh, Gia) values(@TenMon, @NhomMon, @DonViTinh, @Gia)";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);
                OleDbParameter thamso;

                thamso = new OleDbParameter("@TenMon", OleDbType.VarChar);
                thamso.Value = mon.TenMon;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@NhomMon", OleDbType.VarChar);
                thamso.Value = mon.NhomMon;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@DonViTinh", OleDbType.VarChar);
                thamso.Value = mon.DonViTinh;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@Gia", OleDbType.Integer);
                thamso.Value = mon.Gia;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua;
        }

        public static bool XoaMon(string tenmon)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Delete From MON Where TenMon = @TenMon";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);

                OleDbParameter thamso = new OleDbParameter("@TenMon", OleDbType.VarChar);
                thamso.Value = tenmon;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua;
        }

        public static bool SuaThongTinMon(MonDTO mon)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Update MON Set TenMon = @TenMon, NhomMon = @NhomMon, DonViTinh = @DonViTinh, Gia = @Gia Where MaMon = @MaMon";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);

                OleDbParameter thamso;

                thamso = new OleDbParameter("@TenMon", OleDbType.VarChar);
                thamso.Value = mon.TenMon;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@NhomMon", OleDbType.VarChar);
                thamso.Value = mon.NhomMon;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@DonViTinh", OleDbType.VarChar);
                thamso.Value = mon.DonViTinh;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@Gia", OleDbType.Integer);
                thamso.Value = mon.Gia;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@MaMon", OleDbType.Integer);
                thamso.Value = mon.MaMon;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua;
        }

        public static List<MonDTO> LayDanhSachMon()
        {
            List<MonDTO> dsMon = new List<MonDTO>();

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Select TenMon, NhomMon, Gia From MON";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);

                OleDbDataReader bodoc = lenh.ExecuteReader();
                while (bodoc.Read())
                {
                    MonDTO mon = new MonDTO();
                        mon.TenMon = bodoc.GetString(0);
                    if (!bodoc.IsDBNull(1))
                        mon.NhomMon = bodoc.GetString(1);
                    if (!bodoc.IsDBNull(2))
                        mon.Gia = bodoc.GetInt32(2);

                    dsMon.Add(mon);
                }
            }
            catch (Exception ex)
            {
                dsMon = new List<MonDTO>();
            }

            return dsMon;
        }

        public static MonDTO LayThongTinMon(string tenmon)
        {
            MonDTO mon = new MonDTO();

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Select * From MON Where TenMon = @TenMon";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);

                OleDbParameter thamso = new OleDbParameter("@TenMon", OleDbType.VarChar);
                thamso.Value = tenmon;
                lenh.Parameters.Add(thamso);

                OleDbDataReader bodoc = lenh.ExecuteReader();
                while (bodoc.Read())
                {
                    mon.MaMon = bodoc.GetInt32(0);
                    if (!bodoc.IsDBNull(1))
                        mon.TenMon = bodoc.GetString(1);
                    if (!bodoc.IsDBNull(2))
                        mon.NhomMon = bodoc.GetString(2);
                    if (!bodoc.IsDBNull(3))
                        mon.DonViTinh = bodoc.GetString(3);
                    if (!bodoc.IsDBNull(4))
                        mon.Gia = bodoc.GetInt32(4);
                }
            }
            catch (Exception ex)
            {
                mon = new MonDTO();
            }

            return mon;
        }

        public static MonDTO LayThongTinMon(long mamon)
        {
            MonDTO mon = new MonDTO();

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Select * From MON Where MaMon = @MaMon";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);

                OleDbParameter thamso = new OleDbParameter("@MaMon", OleDbType.Integer);
                thamso.Value = mamon;
                lenh.Parameters.Add(thamso);

                OleDbDataReader bodoc = lenh.ExecuteReader();
                while (bodoc.Read())
                {
                    mon.MaMon = bodoc.GetInt32(0);
                    if (!bodoc.IsDBNull(1))
                        mon.TenMon = bodoc.GetString(1);
                    if (!bodoc.IsDBNull(2))
                        mon.NhomMon = bodoc.GetString(2);
                    if (!bodoc.IsDBNull(3))
                        mon.DonViTinh = bodoc.GetString(3);
                    if (!bodoc.IsDBNull(4))
                        mon.Gia = bodoc.GetInt32(4);
                }
            }
            catch (Exception ex)
            {
                mon = new MonDTO();
            }

            return mon;
        }
    }
}
